from django.views import View

from django.views import View

from User.models import Order
from sportsground.utils import Req
from sportsground.utils.Res import Res
from sportsground.utils.views import LoginRequiredUser


class ShopCarView(LoginRequiredUser, View):
    """ 购物车信息 """

    def get(self, request):
        """ 查询 """
        user = request.user.user
        shopCar = user.shopCars
        result = []
        for item in shopCar:
            # if item['type'] == Order.OrderType.Yard:
            #     yardId = item['id']
            #     yard = VenueSportYard.objects.filter(id=yardId).first()
            #     item['yard'] = None
            #     item['yard'] = yard and yard.Info()
            if item['type'] == Order.OrderType.Goods:
                pass
            elif item['type'] == Order.OrderType.Rent:
                pass
            # elif item['type'] == Order.OrderType.Vip:
            #     pass
            result.append(item)
        return Res.Json(Res.OK, result=result)

    @Req.getJson
    def post(self, request):
        """ 添加或更改 """
        type = request.json_dict.get('type')
        id = request.json_dict.get('id')
        number = request.json_dict.get('number')

        user = request.user.user

        if type not in Order.OrderType.list():
            return Res.Json(Res.ParamErr)

        user.addShopCar(id,type,number)
        return Res.Json(Res.OK)

    @Req.getJson
    def delete(self, request):
        """ 删除 """
        id = request.json_dict.get('id')
        type = request.json_dict.get('type')

        user = request.user.user

        if type not in Order.OrderType.list():
            return Res.Json(Res.ParamErr)

        user.popShopCar(id=id, type=type)
        return Res.Json(Res.OK)
